package com.liming.product.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liming.common.exception.BusinessException;
import com.liming.common.pojo.dto.PageDTO;
import com.liming.common.pojo.dto.product.AddBrandDTO;
import com.liming.common.pojo.dto.product.UpdateBrandDTO;
import com.liming.common.pojo.entity.product.Brand;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.product.BrandVO;
import com.liming.common.util.BeanUtils;
import com.liming.common.util.mybatisplus.MPUtil;
import com.liming.product.enums.SelectedEnum;
import com.liming.product.mapper.BrandMircoMapper;
import com.liming.product.service.BrandMircoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liming
 * @date 2025/1/15
 */
@Service
public class BrandMircoServiceImpl extends ServiceImpl<BrandMircoMapper, Brand>
        implements BrandMircoService, MPUtil<Brand> {

    @Override
    public List<BrandVO> selectedBrand() {
        return lambdaQuery().eq(Brand::getSelected, 1).list().stream().map(brand -> BeanUtils.toBean(brand, BrandVO.class)).toList();
    }

    @Override
    public Boolean saveBrand(AddBrandDTO dto) {
        Brand brand = new Brand();
        org.springframework.beans.BeanUtils.copyProperties(dto, brand);
        Assert.isTrue(save(brand), () -> new BusinessException("品牌保存失败"));
        return true;
    }

    @Override
    public PageVO<BrandVO> pageBrand(PageDTO pageDTO, String name) {
        IPage<Brand> page = page(setPage(pageDTO), getLQW().like(StrUtil.isNotBlank(name), Brand::getName, name));
        PageVO<BrandVO> pageVO = new PageVO<>();
        org.springframework.beans.BeanUtils.copyProperties(page.convert(brand -> {
            BrandVO vo = new BrandVO();
            org.springframework.beans.BeanUtils.copyProperties(brand, vo);
            return vo;
        }), pageVO);
        return pageVO;
    }

    @Override
    public BrandVO get(Long id) {
        Brand brand = getById(id);
        BrandVO vo = new BrandVO();
        org.springframework.beans.BeanUtils.copyProperties(brand, vo);
        return vo;
    }

    @Override
    public BrandVO getByName(String name) {
        return BeanUtils.toBean(lambdaQuery().eq(Brand::getName, name).one(), BrandVO.class);
    }

    @Override
    public Boolean updateBrand(UpdateBrandDTO updateBrandDTO) {
        Brand update = new Brand();
        org.springframework.beans.BeanUtils.copyProperties(updateBrandDTO, update);
        Assert.isTrue(updateById(update), () -> new BusinessException("品牌更新失败"));
        return true;
    }

    @Override
    public Boolean updateSelectedById(Long id) {
        Brand brand = lambdaQuery().eq(Brand::getId, id).one();
        boolean updateFlag = lambdaUpdate()
                .eq(Brand::getId, id)
                .set(ObjectUtil.equal(brand.getSelected(), SelectedEnum.NO.getCode()), Brand::getSelected, SelectedEnum.YES.getCode())
                .set(ObjectUtil.equal(brand.getSelected(), SelectedEnum.YES.getCode()), Brand::getSelected, SelectedEnum.NO.getCode())
                .update();
        Assert.isTrue(updateFlag, () -> new BusinessException("品牌精选更新失败"));
        return true;
    }
}
